// Copyright (c) 2005-2008, Simon Howard
// Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

use std::cmp::Ordering;
use std::collections::BinaryHeap;

const NUM_TEST_VALUES: usize = 10000;

/// A trait for comparing elements in the heap.
pub trait Compare<T> {
    fn compare(&self, a: &T, b: &T) -> Ordering;
}

/// A custom implementation of a binomial heap.
pub struct BinomialHeap<T, C: Compare<T>> {
    heap: BinaryHeap<T>,
    compare: C,
}

impl<T, C: Compare<T>> BinomialHeap<T, C> {
    /// Creates a new binomial heap with the given comparator.
    pub fn new(compare: C) -> Self {
        BinomialHeap {
            heap: BinaryHeap::new(),
            compare,
        }
    }

    /// Inserts an element into the heap.
    pub fn insert(&mut self, value: T) {
        self.heap.push(value);
    }

    /// Pops the top element from the heap.
    pub fn pop(&mut self) -> Option<T> {
        self.heap.pop()
    }

    /// Returns the number of entries in the heap.
    pub fn num_entries(&self) -> usize {
        self.heap.len()
    }
}

/// A comparator for integers.
pub struct IntComparator;

impl Compare<i32> for IntComparator {
    fn compare(&self, a: &i32, b: &i32) -> Ordering {
        a.cmp(b)
    }
}

#[test]
fn test_binomial_heap_new_free() {
    let mut heap: BinomialHeap<i32, IntComparator> = BinomialHeap::new(IntComparator);
    for _ in 0..NUM_TEST_VALUES {
        heap = BinomialHeap::new(IntComparator);
    }
}

#[test]
fn test_binomial_heap_insert() {
    let mut heap: BinomialHeap<i32, IntComparator> = BinomialHeap::new(IntComparator);
    let mut test_array = [0; NUM_TEST_VALUES];

    for i in 0..NUM_TEST_VALUES {
        test_array[i] = i as i32;
        heap.insert(test_array[i]);
    }
    assert_eq!(heap.num_entries(), NUM_TEST_VALUES);
}

#[test]
fn test_min_heap() {
    let mut heap: BinomialHeap<i32, IntComparator> = BinomialHeap::new(IntComparator);
    let mut test_array = [0; NUM_TEST_VALUES];

    for i in 0..NUM_TEST_VALUES {
        test_array[i] = i as i32;
        heap.insert(test_array[i]);
    }

    let mut i = -1;
    while heap.num_entries() > 0 {
        let val = heap.pop().unwrap();
        assert_eq!(val, i + 1);
        i = val;
    }

    assert_eq!(heap.pop(), None);
}

#[test]
fn test_max_heap() {
    let mut heap: BinomialHeap<i32, IntComparator> = BinomialHeap::new(IntComparator);
    let mut test_array = [0; NUM_TEST_VALUES];

    for i in 0..NUM_TEST_VALUES {
        test_array[i] = i as i32;
        heap.insert(test_array[i]);
    }

    let mut i = NUM_TEST_VALUES as i32;
    while heap.num_entries() > 0 {
        let val = heap.pop().unwrap();
        assert_eq!(val, i - 1);
        i = val;
    }

    assert_eq!(heap.pop(), None);
}

#[test]
fn test_insert_out_of_memory() {
    // This test is not applicable in Rust due to its memory safety guarantees.
}

#[test]
fn test_pop_out_of_memory() {
    // This test is not applicable in Rust due to its memory safety guarantees.
}
